﻿using LeSoft.Application.Code;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;

namespace LeSoft.Application.Web.Areas.FinancialManage.Controllers
{
    public class EnergyMeterFeeController : MvcControllerBase
    {
        private FMEnergyMeterBillBLL bll = new FMEnergyMeterBillBLL();
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Index()
        {
            var AuditSetting = new SettingBLL().GetWorkAuditEntityByCode(((int)CodeRuleEnum.FMEnergyMeterBill).ToString());
            ViewBag.AuditSetting = AuditSetting == null ? "" : AuditSetting.Id;
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit)]
        public ActionResult Detail()
        {
            return View();
        }

        #endregion

        #region 查询数据

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetForm, "财务-抄表实体", "GetFrom")]
        public ActionResult GetFrom(string keyValue)
        {
            var obj = new object();
            var data = bll.GetEntity(keyValue);
            obj = new { data = data };
            if (!string.IsNullOrEmpty(data.EMBillId))
            {
                var EMBEntity = new EnergyBLL().GetEnergyMeterBill(data.EMBillId);
                var EMBDetail = new EnergyBLL().GetReadingEnergyMeterList(data.EMBillId).Join(new EnergyBLL().GetInstanceList(), x => x.EnergyMeterInstanceId, y => y.Id, (x, y) => new
                {
                    x.Id,
                    x.EnergyMeterInstanceId,
                    x.AdjustVolume,
                    x.BatchDate,
                    x.BatchNo,
                    x.LastBatchDate,
                    x.LastScaleDate,
                    x.LastScaleNum,
                    x.Price,
                    x.PStructId,
                    x.ScaleDate,
                    x.ScaleNum,
                    x.State,
                    x.UnitPrice,
                    x.Volume,
                    y.Name,
                    y.Type,
                    y.Code,
                    y.PSMemo
                }).Join(new PStructCache().GetList(), x => x.PStructId, y => y.Id, (x, y) => new
                {
                    x.Id,
                    x.EnergyMeterInstanceId,
                    x.AdjustVolume,
                    x.BatchDate,
                    x.BatchNo,
                    x.LastBatchDate,
                    x.LastScaleDate,
                    x.LastScaleNum,
                    x.Price,
                    x.PStructId,
                    x.ScaleDate,
                    x.ScaleNum,
                    x.State,
                    x.UnitPrice,
                    x.Volume,
                    x.Name,
                    x.Type,
                    x.Code,
                    x.PSMemo,
                    y.AllName
                });
                var FeeItems = new ReceivableFeeItemsBLL().GetList(keyValue).Join(new PStructCache().GetList(), x => x.PStructId, y => y.Id, (x, y) => new
                {
                    x.Id,
                    x.State,
                    x.AccMonth,
                    x.BeginDate,
                    x.Coefficient,
                    x.Cost,
                    x.CustomerId,
                    x.DelayBeginDate,
                    x.DelayRate,
                    x.PStructId,
                    x.EndDate,
                    x.LimitDate,
                    x.Quantity,
                    x.UnitPrice,
                    x.ReceiveAmount,
                    x.ReceivedAmount,
                    x.ReceiveMonth,
                    x.TaxAmount,
                    x.TaxRate,
                    x.ItemId,
                    y.AllName
                }).Join(new CustomerInfoBLL().GetList(), x => x.CustomerId, y => y.Id, (x, y) => new
                {
                    x.Id,
                    x.State,
                    x.AccMonth,
                    x.BeginDate,
                    x.Coefficient,
                    x.Cost,
                    x.CustomerId,
                    x.DelayBeginDate,
                    x.DelayRate,
                    x.PStructId,
                    x.EndDate,
                    x.LimitDate,
                    x.Quantity,
                    x.UnitPrice,
                    x.ReceiveAmount,
                    x.ReceivedAmount,
                    x.ReceiveMonth,
                    x.TaxAmount,
                    x.ItemId,
                    x.TaxRate,
                    x.AllName,
                    y.Name
                }).Join(new FeeItemBLL().GetList(), x => x.ItemId, y => y.Id, (x, y) => new
                {
                    x.Id,
                    x.State,
                    x.AccMonth,
                    x.BeginDate,
                    x.Coefficient,
                    x.Cost,
                    x.CustomerId,
                    x.DelayBeginDate,
                    x.DelayRate,
                    x.PStructId,
                    x.EndDate,
                    x.LimitDate,
                    x.Quantity,
                    x.UnitPrice,
                    x.ReceiveAmount,
                    x.ReceivedAmount,
                    x.ReceiveMonth,
                    x.TaxAmount,
                    x.TaxRate,
                    x.AllName,
                    x.Name,
                    Code = y.Name
                });
                obj = new { data = data, data1 = EMBEntity, data2 = EMBDetail, data3 = FeeItems };
            }
            return Content(obj.ToJson());
        }
        [HttpPost]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "财务-抄表", "GetPageList")]
        public ActionResult GetPageList(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            var data = bll.GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }


        [HttpPost]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "财务抄表审核记录", "GetAudit")]
        public ActionResult GetAudit(string BillId)
        {
            var AuditSetting = new SettingBLL().GetWorkAuditEntityByCode(((int)CodeRuleEnum.FMEnergyMeterBill).ToString());
            var obj = new List<object>();
            if (AuditSetting != null)
            {
                var AuditMemoentity = AuditSetting.AuditMemo.ToList<WorkAuditMemoEntity>();
                var AuditRecord = new AuditRecordBLL().GetList(BillId);
                foreach (var item in AuditMemoentity)
                {
                    var data = AuditRecord.FirstOrDefault(t => t.Level == item.Level);
                    if (data != null)
                    {
                        var Acc = new AccountBLL().GetEntity(data.AuditMan);
                        obj.Add(new { item.RoleType, item.RoleId, Level = item.Level, data.AuditTime, data.AuditState, AuditMan = Acc != null ? Acc.Name : data.AuditMan, data.AuditMemo });
                    }
                    else
                        obj.Add(new { item.RoleType, item.RoleId, Level = item.Level });
                }
            }
            return Content(obj.ToJson());
        }
        #endregion

        //[HttpGet]
        //[HandlerAuthorize(PermissionMode.Ignore)]
        //[HandlerAction(OperationType.GetList, "财务-抄表", "GetFormInfo")]
        //public ActionResult GetFormInfo(string keyValue)
        //{
        //    var data = new
        //    {
        //        Bill = new EnergyBLL().GetEnergyMeterBill(keyValue),
        //        List = new EnergyBLL().GetReadingEnergyMeterList(keyValue)
        //    };
        //    return Content(data.ToJson());
        //}

        #region 审核
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="auditRecordEntity"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "审核", "AuditForm")]
        public ActionResult AuditForm(string keyValue, AuditRecordEntity auditRecordEntity)
        {
            try
            {
                bll.AuditForm(keyValue, auditRecordEntity);
                return Success("操作成功！");
            }
            catch (Exception e)
            {
                return Error("操作失败!" + e.Message);
            }
        }
        #endregion


    }
}
